package com.edou.member.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.edou.base.page.PageResult;
import com.edou.member.dao.IMemberRoleDao;
import com.edou.member.dao.IMemberRoleRightDao;
import com.edou.member.entity.MemberRole;
import com.edou.member.entity.MemberRoleRight;
import com.edou.member.service.IMemberRoleService;

import com.edou.member.dto.MemberRoleCondition;

/**
 * 会员角色
 * @creator     
 * @create-time 2016/5/13 21:54:38
 * @email admin@edou.com
 * @company www.edou.com
 * @version 1.0
 */
@Service("memberRoleServiceImpl")
@Transactional(value = "transactionManager")
public class MemberRoleServiceImpl implements IMemberRoleService{

	@Resource(name = "memberRoleDaoImpl")
	private IMemberRoleDao memberRoleDao;
	
	@Resource(name = "memberRoleRightDaoImpl")
	private IMemberRoleRightDao memberRoleRightDao;

	/**
	 * 查询所有会员角色
	 */
	public List<MemberRole> listMemberRoleAll(){
		return memberRoleDao.listMemberRoleAll();
	}

	/**
	 * 分页查询
	 * @param pageResult 分页对象
	 * @param condition 查询条件类
	 */
	public void listMemberRoleByPage(PageResult<MemberRole> pageResult,MemberRoleCondition condition){
		int rows = memberRoleDao.listMemberRoleByPageCount(condition);
		pageResult.setRows(rows);

		RowBounds rowBounds = new RowBounds(pageResult.getCurrentPageIndex(),pageResult.getPageSize());
		List<MemberRole> list = memberRoleDao.listMemberRoleByPage(rowBounds,condition);
		pageResult.setResult(list);
	}

	/**
	 * 根据ID查询
	 * @param id 主键
	 * @return 会员角色
	 */
	public MemberRole getMemberRoleById(String id){
		return memberRoleDao.getMemberRoleById(id);
	}

	/**
	 * 新增
	 * @param item 会员角色
	 */
	public void add(MemberRole item){
		if(StringUtils.isBlank(item.getId())){
			//新增
			memberRoleDao.add(item);
		}else{
			MemberRole oldItem = memberRoleDao.getMemberRoleById(item.getId());
			oldItem.setName(item.getName());
			oldItem.setCode(item.getCode());
			oldItem.setRemark(item.getRemark());
			//修改
			memberRoleDao.update(oldItem);
			//清空角色权限
			memberRoleRightDao.delByRoleId(item.getId());
		}
		
		//保存角色权限
		String[] rightIds = item.getRightIds().split(",");
		for(String rightId: rightIds){
			MemberRoleRight roleRight = new MemberRoleRight();
			roleRight.setRightId(rightId);
			roleRight.setRoleId(item.getId());
			memberRoleRightDao.add(roleRight);
		}
	}

	/**
	 * 修改
	 * @param item 会员角色
	 */
	public void update(MemberRole item){
		memberRoleDao.update(item);
	}

	/**
	 * 根据ID集合批量删除
	 * @param ids ID集合
	 */
	public void delByIds(String[] ids){
		memberRoleDao.delByIds(ids);
	}

	@Override
	public List<MemberRole> listMemberRoleByUserId(String memberUserId) {
		return memberRoleDao.listMemberRoleByUserId(memberUserId);
	}

	/**
	 * 根据会员角色代码查询会员信息
	 */
	public MemberRole getMemberRoleByCode(String code) {
		return memberRoleDao.getMemberRoleByCode(code);
	}

	@Override
	public boolean checkCodeRepeat(MemberRole item) {
		int count = memberRoleDao.getCountByCode(item);
		return count > 0;
	}
}

